// Package cmd @author <chengjiang@buffalo-robot.com>
// @date 2023/1/17
// @note
package cmd

import (
	"deploy-tools/postfile/common/strings"
	"encoding/json"
	"fmt"
	"github.com/spf13/cobra"
)

// publish represents the base command when called without any subcommands
var publish = &cobra.Command{
	Use:   "publish [flags]",
	Short: "publish comand, used to post the file to web & oss",
	Long: `publish, 
used to post the file to web and oss.
	publish：
		1. publish: combine the post & oss.`,
	// Uncomment the following line if your bare application
	// has an action associated with it:
	Run: func(cmd *cobra.Command, args []string) {
		println("正在post 相关信息，loading...")
		err := Post()
		if err != nil {
			println("Oops，发生错误...")
			handleError(err)
		}
		println("正在上传相关文件，loading...")
		err = OSS()
		if err != nil {
			println("Oops，发生错误...")
			handleError(err)
		}
	},
	//RunE: func(cmd *cobra.Command, args []string) error {
	//	return errors.New("something error by using the post command")
	//},
}

func init() {
	// Here you will define your flags.
	// Cobra supports persistent flags, which, if defined here,
	// will be global for your application.
	// post
	publish.Flags().StringVarP(&file, "filename", "f", "", "this is for oss file name")
	publish.PersistentFlags().StringVarP(&repo, "reponame", "r", "", "for repository name")
	publish.PersistentFlags().StringVarP(&project, "proname", "p", "", "for project name")
	publish.PersistentFlags().StringVarP(&branch, "branchname", "b", "", "for branch name")
	publish.Flags().StringVarP(&tagI, "tagid", "t", "", "tag id")
	publish.Flags().StringVarP(&changeL, "changelog", "c", "", "change log")
	publish.Flags().StringVarP(&remar, "remark", "m", "", "remark")
	// oss
	publish.PersistentFlags().StringVarP(&objname, "objname", "o", "", "on the oss system, the file path with name")
	publish.Flags().StringVarP(&localfilefullname, "localfilefullname", "l", "", "the local absolute file path with the name.")
	//publish.Flags().StringVarP(&Conf.posturl.token, "token", "n", "", "U-Token for auth")
}

func Post() error {
	sbUrl := strings.NewStrBuilder()
	sbUrl.StrAppend(Conf.posturl.base).
		StrAppend(Conf.posturl.suffix)
	println("now, post to the url: ", sbUrl.StrAppend(", method: ").StrAppend(Conf.posturl.method).ToString())
	println("the data: ")
	postObjMap[fileName] = file
	postObjMap[repoName] = repo
	postObjMap[projectName] = project
	postObjMap[branchName] = branch
	postObjMap[tagID] = tagI
	postObjMap[changeLog] = changeL
	postObjMap[remark] = remar
	//
	postJson, _ := json.Marshal(postObjMap)
	fmt.Println(string(postJson))
	// post to url:
	err := postUrl(sbUrl, postJson)
	if err != nil {
		handleError(err)
	}
	return err
}

func OSS() error {
	// uploadFile
	return uploadFile()
}

func forNotify(noti *Config) *Config {
	//
	noti.notify.Repo = postObjMap[repoName]
	noti.notify.Project = postObjMap[projectName]
	noti.notify.Branch = postObjMap[branchName]
	noti.notify.ObjName = objname
	fmt.Println("the notify: ", Conf.notify)
	return &Config{
		notify: noti.notify,
	}
}
